/** 
 * @author xxz 
 * @version 1.0.0  
 * @依赖 jquery.js
 */
function addMainModels(mainmodel){
	var mainNav = "";
	for(var i = mainmodel.length-1; i >= 0; i--){
		if(i == 0){
			mainNav += "<div class='head-menu head-menu-select' code='" + mainmodel[i].code + "' pageurl='" + mainmodel[i].url + "'>" + mainmodel[i].value + "</div>";
		}else{
			mainNav += "<div class='head-menu' code='" + mainmodel[i].code + "' pageurl='" + mainmodel[i].url + "'>" + mainmodel[i].value + "</div>";
		}
	}
	$("#main-nav").empty();
	$("#main-nav").append(mainNav);
	
	$("#main-body").attr("src",mainmodel[0].url);
	changeUrl(function(){
		if(mainmodel[0].value == "位置服务"){
			addSecPostionModels(mainmodel[0].children);
		}else{
			addSecModels(mainmodel[0].children);
		}
	});
	
	$(".head-menu").unbind("click");
	$(".head-menu").click(function(){
		$(".head-menu").removeClass("head-menu-select");
		$(this).addClass("head-menu-select");
		var index = $(this).index();
		var tot = $(".head-menu").length;
		var name = $(this).text();
		var pageurl = $(this).attr("pageurl");
		$("#main-body").attr("src",pageurl);
		changeUrl(function(){
			if(name == "位置服务"){
				addSecPostionModels(mainmodel[tot - index - 1].children);
			}else{
				addSecModels(mainmodel[tot - index - 1].children);
			}
		});
	});
}

function addSecModels(secmodel){
	var secNav = "";
	for(var i = 0; i < secmodel.length; i++){
		secNav += "<div class='left-menu' model='" + secmodel[i].value + "'>" +
						"<div class='left-menu-img'>" +
							"<img src='" + secmodel[i].icon + "' width='100%' height='100%'>" +
						"</div>" +
						"<div class='left-menu-text'>" + secmodel[i].value + "</div>" +
						"<div class='menu-right'></div>" +
					"</div>" +
					"<div class='left-menu-sec' id='"+secmodel[i].code+"'>";
		if(secmodel[i].children != undefined){
			for(var j = 0; j < secmodel[i].children.length; j++){
				secNav += "<div class='menu-sec' title='" + secmodel[i].children[j].value + "'>" +
				"<div class='sec-left-img'></div>" +
				"<div class='sec-left-text'>" + secmodel[i].children[j].value + "</div>" +
				"</div>";
			}
		}
		secNav += "</div>";
	}
	$("#main-body").contents().find(".left-content").empty();
	$("#main-body").contents().find(".left-content").append(secNav);
	$("#main-body").contents().find("#" +secmodel[0].code).show();
	if(secmodel[0].children != undefined){
		$("#main-body").contents().find(".menu-sec[title='"+ secmodel[0].children[0].value +"']").addClass("menu-sec-select");
		$("#main-body").contents().find("#right-iframe").attr("src",secmodel[0].children[0].url);
	}else{
		$("#main-body").contents().find(".menu-sec[title='"+ secmodel[0].value +"']").addClass("menu-sec-select");
		$("#main-body").contents().find("#right-iframe").attr("src",secmodel[0].url);
	}
	
	$("#main-body").contents().find(".left-menu").unbind("click");
	$("#main-body").contents().find(".left-menu").click(function(){
		$("#main-body").contents().find(".left-menu-sec").hide();
		var model = $("#main-body").contents().find(this).attr("model");
		for(var i = 0; i < secmodel.length; i++){
			if(model == secmodel[i].value){
				$("#main-body").contents().find("#" + secmodel[i].code).show();
				var Hei = $("#main-body").contents().find("#" + secmodel[i].code + " .menu-sec").length * $("#main-body").contents().find(".menu-sec").height();
				$("#main-body").contents().find("#" + secmodel[i].code).height(Hei);
				break;
			}
		}
	});
	$("#main-body").contents().find(".menu-sec").unbind("click");
	$("#main-body").contents().find(".menu-sec").click(function(){
		$("#main-body").contents().find(".menu-sec").removeClass("menu-sec-select");
		$("#main-body").contents().find(this).addClass("menu-sec-select");
		var model = $("#main-body").contents().find(this).attr("title");
		for(var i = 0; i < secmodel.length; i++){
			if(secmodel[i].children != undefined){
				for(var j = 0; j < secmodel[i].children.length; j++){
					if(model == secmodel[i].children[j].value){
						$("#main-body").contents().find("#right-iframe").attr("src",secmodel[i].children[j].url);
						break;
					}
				}
			}
		}
		
	});
}
function addSecPostionModels(secmodel){
	var secNav = "";
	for(var i = 0; i < secmodel.length; i++){
		secNav += "<li model='" + secmodel[i].value + "'>" +
						"<div class='quality-left-page'>" +
							"<img src='" + secmodel[i].icon + "' width='100%' height='100%'>" +
						"</div>" +
						"<div class='btns-text'>" + secmodel[i].value + "</div>" +
					"</li>";
	}
	$("#main-body").contents().find(".left-menu-ul").empty();
	$("#main-body").contents().find(".left-menu-ul").append(secNav);
	$("#main-body").contents().find(".left-menu-ul li[model='"+ secmodel[0].value +"']").addClass("left-menu-liselect");
	$("#main-body").contents().find("#aposition-body").attr("src",secmodel[0].url);
	
	$("#main-body").contents().find(".left-menu-ul li").unbind("click");
	$("#main-body").contents().find(".left-menu-ul li").click(function(){
		$("#main-body").contents().find(".left-menu-ul li").removeClass("left-menu-liselect");
		$("#main-body").contents().find(this).addClass("left-menu-liselect");
		var model = $("#main-body").contents().find(this).attr("model");
		for(var i = 0; i < secmodel.length; i++){
			if(model == secmodel[i].value){
				$("#main-body").contents().find("#aposition-body").attr("src",secmodel[i].url);
				break;
			}
		}
	});
	
}

function changeUrl(init){
	var oFrm = document.getElementById('main-body'); 
	oFrm.onload = oFrm.onreadystatechange = function(){ 
		if(this.readyState && this.readyState != 'complete'){
			return; 
		}else{ 
			 if(typeof init === "function"){
                init.call();
            }
		} 
	};
}